﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class CommentBLL
  {
    public Comment GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Comments.Where(p => p.Id == id).FirstOrDefault();
      }
    }
    public List<Comment> GetByProductId(string productId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Comments.Where(p => p.ProductId == productId && p.Level == 1).OrderByDescending(p => p.DateUpdate).ToList();
      }
    }
    public List<Comment> GetByProductId(string productId, int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Comments.Where(p => p.ProductId == productId && p.Level == 1).OrderByDescending(p => p.DateUpdate).Take(top).ToList();
      }
    }

    public List<Comment> GetByCommentId(int commentId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Comments.Where(p => p.CommentId == commentId).OrderByDescending(p => p.DateSend).ToList();
      }
    }
    public bool isHasChild(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        var list = data.Comments.Where(p => p.CommentId == id).ToList();
        if (list.Count == 0) return false;
        else return true;
      }
    }
    public int GetTotalComment(string productId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        var list =  data.Comments.Where(p => p.ProductId == productId).ToList();
        return list.Count;
      }
    }
    public void AddComent(Comment obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Comments.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }
    public void DeleteReply(int commentId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<Comment> list = data.Comments.Where(p => p.CommentId == commentId).ToList();
        data.Comments.DeleteAllOnSubmit(list);
        data.SubmitChanges();
      }
    }
    public void DeleteComment(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Comment obj = data.Comments.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
        {
          DeleteReply(id);
          data.Comments.DeleteOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }
    public void DeleteByProductId(string productId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<Comment> obj = data.Comments.Where(p => p.ProductId == productId).ToList();
        if (obj.Count > 0 )
        {
          data.Comments.DeleteAllOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }
    public void DeleteByUserId(int userId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<Comment> obj = data.Comments.Where(p => p.UserID == userId).ToList();
        if (obj.Count > 0)
        {
          data.Comments.DeleteAllOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }

    public void UpdateTime(Comment obj)
    {
      using (var data= new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Comment comment = data.Comments.Where(p => p.Id == obj.Id).FirstOrDefault();
        if (obj != null)
        {
          comment.DateUpdate = obj.DateUpdate;
          data.SubmitChanges();
        }
      }
    }
  }
}
